package com.preguicoso.client.login;


import com.google.gwt.core.client.GWT;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.user.client.Cookies;
import com.google.gwt.user.client.History;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.Widget;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.user.client.ui.InlineHyperlink;
import com.google.gwt.user.client.ui.InlineLabel;
import com.preguicoso.client.telaInicial.EscolhaCep;
import com.preguicoso.client.telaInicial.lightBox;
import com.preguicoso.shared.entities.UsuarioBean;
import com.google.gwt.user.client.ui.HTMLPanel;
import com.google.gwt.uibinder.client.UiHandler;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.user.client.ui.Label;

public class Login extends Composite{

	private static Login instance = null;

	private final LoginServiceAsync loginService = GWT
			.create(LoginService.class);
	
	public static Login getInstance() {
		if (instance == null) {
		
				instance = new Login();
		}
		return refresh();
	}
	
	private static LoginUiBinder uiBinder = GWT.create(LoginUiBinder.class);
	@UiField InlineHyperlink login;
	@UiField
	static InlineLabel Localidade;
	@UiField
	static InlineHyperlink mudar;
	@UiField HTMLPanel panel;
	@UiField InlineHyperlink logout;
	@UiField InlineLabel email;
	
	boolean logado=false;

	interface LoginUiBinder extends UiBinder<Widget, Login> {
	}

	public Login() {
		initWidget(uiBinder.createAndBindUi(this));
		
		atualizaToken();
		login.setVisible(true);
		logout.setVisible(false);
		email.setVisible(false);
		mudar.setVisible(false);
		isLogado();
		
		EscolhaCep cep = EscolhaCep.getInstance();
		if(cep.getBairro()==null)
		{
			History.newItem("cep");
			Localidade.setText("");
			mudar.setVisible(false);
		}
		else{
			Localidade.setText(cep.getBairro());
			mudar.setVisible(true);
		}
	}
	public void atualizaToken() {
		String token = History.getToken();
		login.setTargetHistoryToken(token);
		//mudar.setTargetHistoryToken(token);
		logout.setTargetHistoryToken(token);
	}
	public void Logar(String login, String pass) {
		loginService.logarUsuario(login, pass, new AsyncCallback<String>() {
			
			@Override
			public void onSuccess(String result) {
				if(result==null){
					lightBox.getInstance().hide();
					isLogado();
					atualizar();
				}
				else{
					lightBox.getInstance().show(new HTMLPanel("<h2>"+result+"</h2>"), 300, 100);
					atualizar();
				}
			}
			
			@Override
			public void onFailure(Throwable caught) {
				lightBox.getInstance().show(new HTMLPanel("<h2>Problemas</h2>"), 300, 100);
				
			}
		});
	}
	
	public void setLogado(){
		logado = true;
		loginService.getUsuarioLogado(new AsyncCallback<UsuarioBean>() {
			
			@Override
			public void onSuccess(UsuarioBean result) {
				email.setText(result.getEmail());
				email.setVisible(true);
				Localidade.setText(result.getBairro());
				logado =true; 
			}
			
			@Override
			public void onFailure(Throwable caught) {
				logado = false;
				login.setVisible(true);
				logout.setVisible(false);
				email.setVisible(false);
			//	mudar.setVisible(false);
			}
			
		});
	}
	
	private boolean isLogado() {
		if(!logado)
			loginService.isUsuarioLogado(new AsyncCallback<Boolean>() {
				
				@Override
				public void onSuccess(Boolean result) {
					if(result){
						showLogado();
						loginService.getUsuarioLogado(new AsyncCallback<UsuarioBean>() {
							
							@Override
							public void onSuccess(UsuarioBean result) {
								email.setText(result.getEmail());
								email.setVisible(true);
								logado = true;
								
								showLogado();
							}
							
							@Override
							public void onFailure(Throwable caught) {
								logado = false;
								showLogout();
							}
						});
					}else{
						showLogout();
					}
				}
				
				@Override
				public void onFailure(Throwable caught) {
					// TODO Auto-generated method stub
					logado = false;
				}
			});
		else{
			logado = true;
			return true;
		}
		return logado;
	}
	
	public static Login refresh(){
		EscolhaCep cep = EscolhaCep.getInstance();
		if(cep.getBairro()==null)
		{
			History.newItem("cep");
			mudar.setVisible(false);
		}
		else{
			Localidade.setText(cep.getBairro());
			mudar.setVisible(true);
			//cep.setVisible(false);
		}
		return instance;
	}
	
	public void atualizar(){
		atualizaToken();
		if(logado){
			showLogado();
		}
		else{

			showLogout();
		}
	}
	private void showLogout() {
		logado = false;
		login.setVisible(true);
		logout.setVisible(false);
		email.setVisible(false);
	}
	private void showLogado() {
		login.setVisible(false);
		logout.setVisible(true);
		login.setVisible(false);
		email.setVisible(true);
		//mudar.setVisible(true);
	}

	@UiHandler("mudar")
	void onMudarClick(ClickEvent event) {
		atualizaToken();
		Cookies.removeCookie("bairro");
		mudar.setVisible(false);
		Localidade.setText("");
		//History.newItem("cep");
		refresh();
	}
	
	@UiHandler("login")
	void onLoginClick(ClickEvent event) {
		lightBox box = lightBox.getInstance();
		box.show(new LoginUI(), 489, 144);
		atualizaToken();
	}
	
	@UiHandler("logout")
	void onLogoutClick(ClickEvent event) {
		atualizaToken();
		loginService.fazerLogoutUsuario(new AsyncCallback<Void>() {
			
			@Override
			public void onSuccess(Void result) {
				refresh();
				atualizar();
				logado = false;
				showLogout();
			}
			
			@Override
			public void onFailure(Throwable caught) {
				// TODO Auto-generated method stub
				
			}
		});
	}
}
